<?php

/**
 * PHP versions 5
 * MagicPHP
 * Licensed under The Open Group Test Suite License
 * Redistributions of files must retain the above copyright notice.
 * @author: 	Marcelo Costales
 * @copyright:  Copyright 2011,MagicPHP
 * @link:          
 * @package:    Model
 * @since:      MaginPHP 1.0
 * @license:	GNU General Public License (GPL) 
 * @link:	http://www.phpclasses.org/package/1675-PHP-Parse-ini-files-and-extract-key-values.html 
 * 
 */
?>
<?php

/**
 * @desc: This Class is used for read ini file
 * @author Marcelo Costales
 * @version: 1.0
 */
class Magic_ReadIniFile_Model {
    private static $_instance ; 
    /**
     * @author:aNKIT kHAMBHATA 
     * @desc:This function is used to return self variable 
     */
    public static function callMethod(){
        self::$_instance = new self ; 
        return self::$_instance; 
    }
    /**
     * @author:Marcelo Costales 
     * @desc:This function is used to 
     * @param $IniFile : name of file
     * @param $IniKey : name of the key 
     * @param $IniVar : name of variable name
     * @example:File Structure of test.ini
     * ---------------------------------- 
     * [Company]						|
     * Companyname=xyz					|
     * Cityname=abc						|
     * [Country]						|
     * Countryname=cds					|
     * [Amount]							|
     * Cash=1256.36						|
     * ----------------------------------	
     * 	how to used it   
     * 	$ini = new readini;
     * 	print $ini->ReadIniValue("test.ini", "company", "companyname");
     * ----------------------------------
     * out put
     * abc
     */
    public function readIniValue($IniFile , $IniKey, $IniVar) {
        //echo $IniFile ;
        $this->Ini_Key = "[" . strtolower($IniKey) . "]";
        $this->Ini_Variable = strtolower($IniVar);
        $this->Ini_File = file($IniFile);
        unset($this->Ini_Value);
        for ($Ini_Rec = 0; $Ini_Rec < sizeof($this->Ini_File); $Ini_Rec++) {
            $this->Ini_Temp = trim($this->Ini_File[$Ini_Rec]); // Save the original var value to return it as result
            $this->Ini_Tmp = strtolower($this->Ini_Temp);
            if (substr_count($this->Ini_Tmp, "[") > 0)
                $this->Ini_Ready = 0;
            if ($this->Ini_Tmp == $this->Ini_Key)
                $this->Ini_Ready = 1;

            If ((substr_count($this->Ini_Tmp, "[") == 0) && ($this->Ini_Ready == 1)) {
                if (substr_count($this->Ini_Tmp, $this->Ini_Variable . "=") > 0) {
                    $this->Ini_Value = substr($this->Ini_Temp, strlen($this->Ini_Variable . "="));
                    return $this->Ini_Value;
                }
            }
        }
        if (!$this->Ini_Value) {
            return "ERROR .: Key: [<b>" . strtoupper($IniKey) . "</b>] or Variable: <b>" . strtoupper($IniVar) . "</b>, does not exist in <b>" . strtoupper($IniFile) . "</b> file !";
        }
    }

}

?>